package com.ruoyi.aisite.controller;

import com.ruoyi.aisite.domain.AiComponent;
import com.ruoyi.aisite.service.IAiComponentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AI组件Controller
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/aisite/component")
public class AiComponentController extends BaseController {
    private String prefix = "aisite/component";

    @Autowired
    private IAiComponentService aiComponentService;

    @GetMapping()
    public String component() {
        return prefix + "/component";
    }

    /**
     * 查询AI组件列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AiComponent aiComponent) {
        startPage();
        List<AiComponent> list = aiComponentService.selectAiComponentList(aiComponent);
        return getDataTable(list);
    }

    /**
     * 新增AI组件
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存AI组件
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AiComponent aiComponent) {
        return toAjax(aiComponentService.insertAiComponent(aiComponent));
    }

    /**
     * 修改AI组件
     */
    @GetMapping("/edit/{componentId}")
    public String edit(@PathVariable("componentId") Long componentId, ModelMap mmap) {
        AiComponent aiComponent = aiComponentService.selectAiComponentById(componentId);
        mmap.put("aiComponent", aiComponent);
        return prefix + "/edit";
    }

    /**
     * 修改保存AI组件
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AiComponent aiComponent) {
        return toAjax(aiComponentService.updateAiComponent(aiComponent));
    }

    /**
     * 删除AI组件
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(aiComponentService.deleteAiComponentByIds(Convert.toLongArray(ids)));
    }
}